<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/Language.php'; //include thu vien language
require_once 'CMS/User.php';
//Người tạo : Bùi Thành Nguyên - Team 4
//Module 	: Edit category module
//Chức năng	: Hiển thị form edit category con

class Category_EditLCategoryController extends BaseUserController{

	public function init(){
		parent::init();
		$this->_helper->layout->disableLayout();
		$user = new User();
		$user->init();
		$user->check_login();
		
	}
	
	//Chức năng show form cho người dùng
	public function indexAction(){
	
			/*========== Start Load Language ==========*/
			$language = new Language(APPLICATION_PATH . '/widgets/category/');
			if(isset($_SESSION['language']))
			{
				$language_id = $_SESSION['language'];
				$tbLanguage = new Language_Model_Language();
				$name = $tbLanguage->getNameLanguage($language_id);
				$language->load($name['name']);
			}
			else
				$language->load('Default');		
			/*========== End Load Language ==========*/
				
			/*========== Start Get Text ==============*/
			$this->view->textName 		= $language->get('text_categoryname');
			$this->view->textParent 	= $language->get('text_parentcategory');
			$this->view->textNone 		= $language->get('text_none');
			$this->view->textSave 		= $language->get('text_save');
			$this->view->textError2 	= $language->get('error2');
			$this->view->textError3 	= $language->get('error3');
			$this->view->textError4 	= $language->get('error4');
			$this->view->textError10 	= $language->get('error10');
			$this->view->textError11 	= $language->get('error11');
			$this->view->textError12 	= $language->get('error12');
			$this->view->textError14 	= $language->get('error14');			
			/*========== End Text ==============*/
		/*=========== Get all languages =============*/
			$all_languages = $tbLanguage->getAllLanguages();
			$languages = array();
			foreach ($all_languages as $lang){
				$languages[] = array(
									'id' 	=> $lang['language_id'],
									'name'	=> $lang['name']
									);
			}
			$this->view->languages = $languages;
		/*=========== End get ==================*/
		$path = APPLICATION_PATH."/widgets/category/";
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);//Read config.xml
		$user = new User();
			$user->init();
			$user->check_login();
			$hasmodify = $user->modifiy_widget($xmlconfig->name);
		if($hasmodify=="true")
		{
		}
		else 
		{
			$this->view->error = 2 ;
		}
		//Start--------Khai báo các parameter
		$tbCategory= new Category_Model_Categories();
		//End--------Khai báo các parameter
		
		//lấy type của category cần edit
		if(count($_GET) && isset($_GET["cateid"])) //kiểm tra category type
		{
			$category=$tbCategory->getCategoriesbyId($_GET["cateid"]);
			$cate = array();
			if(isset($category) && count($category) > 0){
				$nameCate = unserialize($category[0]['category_name']);
				$cate[] = array(
										'category_id'	=>	$category[0]['category_id'],
										'category_code'	=>	$category[0]['category_code'],
										'category_name'	=>	$nameCate,
										'parent_id'		=>	$category[0]['parent_id'],
										'is_leaf'		=>	$category[0]['is_leaf'],
										'type'			=>	$category[0]['type']
									);				
			}						
			$parent_categories=$tbCategory->getParentCategoriesbyType($category[0]["type"]);
			$newPCate = array();
			foreach ($parent_categories as $pcate){
				$nameCate = unserialize($pcate['category_name']);
				if(isset($nameCate[$name['name']]) && isset($name)){
					$category_name = $nameCate[$name['name']];
				}else{
					$category_name = $nameCate['Default'];
				}
				$newPCate[] = array(
										'category_id'	=>	$pcate['category_id'],
										'category_code'	=>	$pcate['category_code'],
										'category_name'	=>	$category_name,
										'parent_id'		=>	$pcate['parent_id'],
										'is_leaf'		=>	$pcate['is_leaf'],
										'type'			=>	$pcate['type']
									);
			}			
			$this->view->category=$cate;
			$this->view->parent_categories=$newPCate;
		}
		else
		{
			$this->view->error="-1";
		}
	}
	
}